package com.hex.ds.hdrs.agent.controller;


import com.hex.bigdata.hbda.annotation.AnonymousAccess;
import com.hex.bigdata.hbda.annotation.Log;
import com.hex.bigdata.hbda.base.Page;
import com.hex.bigdata.hbda.utils.SecurityUtils;
import com.hex.ds.hdrs.agent.po.AgentConf;
import com.hex.ds.hdrs.agent.service.AgentConfService;
import com.hex.ds.hdrs.agent.vo.AgentSessionVO;
import com.hex.ds.hdrs.agent.vo.Catalogue;
import com.hex.ds.hdrs.app.po.AppAgentRelationPo;
import com.hex.ds.hdrs.app.vo.AppAgentVo;
import com.hex.ds.hdrs.common.page.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api/agentDef")
@Api(tags = "应用管理")
public class AgentConfController {

    @Autowired
    private AgentConfService agentConfService;

    /**
     * @param agentConf
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加节点")
    @Log("AGENT服务新增")
    public ResponseEntity<Object> add(@RequestBody AgentConf agentConf) {
        log.info("进入了AgentConfController的add方法");
        this.agentConfService.add(agentConf);
        return new ResponseEntity<>("新增成功！", HttpStatus.OK);
    }

    @ApiOperation("检查节点连接状态")
    @PostMapping(value = "/checkConnect")
    @Log("AGENT服务测试连接")
    public ResponseEntity<Object> checkConnect(@RequestBody AgentConf agentConf) {
        log.info("进入了【AgentConfController的checkConnect方法】参数agentConf: {}", agentConf);
        return new ResponseEntity(agentConfService.checkConnect(agentConf), HttpStatus.OK);
    }


    /**
     * 节点状态修改
     *
     * @param agentConf
     * @return
     */
    @ApiOperation("更新节点状态信息")
    @PostMapping(value = "/updateAgentStatus")
    public ResponseEntity<Object> updateAgentStatus(@RequestBody AgentConf agentConf) {
        log.info("进入了【AgentConfController的updateAgentStatus方法】参数agentConf: {}", agentConf.toString());
        this.agentConfService.updateAgentStatus(agentConf);
        return new ResponseEntity("更新成功！", HttpStatus.OK);
    }

    @DeleteMapping
    @ApiOperation("删除节点")
    @Log("AGENT服务删除")
    public ResponseEntity<Object> delete(@RequestBody List<String> ids) {
        log.info("进入了AgentConfController的delete方法");
        this.agentConfService.deleteByIds(ids);
        return new ResponseEntity<>("删除成功!", HttpStatus.OK);

    }

    @PutMapping
    @ApiOperation("修改节点")
    @Log("AGENT服务修改")
    public ResponseEntity update(@RequestBody AgentConf agentConf) {
        log.info("进入了AgentConfController的update方法");
        agentConf.setUpUser(SecurityUtils.getUsername());
        this.agentConfService.update(agentConf);
        return new ResponseEntity("修改成功", HttpStatus.OK);
    }

    @GetMapping("/selectPages")
    @ApiOperation("查询节点")
    public ResponseEntity select(AgentConf agentConf, Page page) {
        log.info("进入了AgentConfController的select方法");
        PageData pageData = new PageData(this.agentConfService.selectAll(agentConf, page), page);
        return new ResponseEntity<>(pageData, HttpStatus.OK);
    }

    /**
     * 节点状态修改
     *
     * @param agentConf
     * @return
     */
    @Log("更新节点状态信息")
    @ApiOperation("更新节点状态信息")
    @PostMapping(value = "/updateAgentHaStatus")
    public ResponseEntity<Object> updateAgentHaStatus(@RequestBody AgentConf agentConf) {
        log.info("进入了【AgentConfController的updateAgentStatus方法】参数agentConf: {}", agentConf.toString());
        this.agentConfService.updateAgentStatus(agentConf);
        return new ResponseEntity("更新成功！", HttpStatus.OK);
    }

    /**
     * 查询某应用管理员是否拥有在该节点下所有app的权限
     *
     * @param agentCode
     * @return
     */
    @Log("查询应用管理员权限")
    @ApiOperation("查询应用管理员权限")
    @GetMapping(value = "/qryHaveApp")
    public ResponseEntity<Object> qryHaveApp(@RequestParam("agentCode") String agentCode) {
        String res = this.agentConfService.qryHaveApp(agentCode);
        return new ResponseEntity(res, HttpStatus.OK);
    }

    /**
     * 根据某节点获取服务器密码
     *
     * @param agentCode
     * @return
     */
    @Log("获取服务器密码")
    @ApiOperation("获取服务器密码")
    @GetMapping(value = "/qryPwdByCode")
    public ResponseEntity<Object> qryPwdByCode(@RequestParam("agentCode") String agentCode) {
        String res = this.agentConfService.qryPwdByCode(agentCode);
        return new ResponseEntity(res, HttpStatus.OK);
    }

    @Log("节点编码查重")
    @ApiOperation("节点编码查重")
    @PostMapping(value = "/checkRepeatCode")
    public ResponseEntity<Object> checkRepeatCode(@RequestBody String agentCode) {
        log.info("进入了【AgentConfController的checkRepeatCode方法】");
        return new ResponseEntity(this.agentConfService.checkRepeatCode(agentCode), HttpStatus.OK);
    }

    @Log("查询节点资源信息")
    @ApiOperation("查询节点资源信息")
    @PostMapping(value = "/chartData")
    public ResponseEntity<Object> chartData(@RequestBody Integer time, String type) {
        log.info("time: {}", time);
        return new ResponseEntity(this.agentConfService.chartData(time, type), HttpStatus.OK);
    }

    @Log("查询节点列表")
    @ApiOperation("查询节点列表")
    @GetMapping(value = "/selectCodes")
    public ResponseEntity<Object> selectCodes() {
        log.info("进入了【AgentConfController的selectCodes方法】");
        return new ResponseEntity(this.agentConfService.selectCodes(), HttpStatus.OK);
    }


    /**
     * 节点信息查询
     *
     * @return
     */
    @ApiOperation("节点查询")
    @GetMapping("/qryNodeInfo")
    public ResponseEntity<Object> qryNodeInfo(@RequestParam String appCode) {
        log.debug("【进入AppConfController的qryGroupCode】");
        List<AppAgentRelationPo> list = this.agentConfService.qryNodeInfo(appCode);
        return new ResponseEntity(list, HttpStatus.OK);
    }

    @Log("节点名称查重")
    @ApiOperation("节点名称查重")
    @PostMapping(value = "/checkAgentName")
    public ResponseEntity<Object> checkAgentName(@RequestBody String agentName) {
        log.info("进入了【AgentConfController的checkAgentName方法】参数agentName: {}", agentName);
        return new ResponseEntity(this.agentConfService.checkAgentName(agentName), HttpStatus.OK);
    }


    @Log("节点IP查重")
    @ApiOperation("节点IP查重")
    @PostMapping(value = "/checkAgentIP")
    public ResponseEntity<Object> checkAgentIP(@RequestBody String agentIp) {
        log.info("进入了【AgentConfController的checkAgentIP方法】参数agentIp: {}", agentIp);
        return new ResponseEntity(this.agentConfService.checkAgentIP(agentIp), HttpStatus.OK);
    }

    @Log("获取节点列表不分页")
    @ApiOperation("获取节点列表不分页")
    @GetMapping(value = "/getAgentList")
    public ResponseEntity<Object> getAgentList() {
        log.debug("进入【AgentConfController的getAgentList方法】");
        List<AgentConf> agentList = this.agentConfService.qryAgentConfList();
        return new ResponseEntity<>(agentList, HttpStatus.OK);
    }

    @Log("根据AgentCode获取session中的task信息")
    @ApiOperation("根据AgentCode获取session中的task信息")
    @PostMapping(value = "/getAgentSessionTask")
    public ResponseEntity<Object> getAgentSessionTask(@RequestBody String agentCode, Page page) {
        log.debug("进入【AgentConfController的getAgentSessionTask方法】参数jsonData: {}", agentCode);
        PageData pageData = new PageData(this.agentConfService.selectPageBySessionList(agentCode, page), page);
        return new ResponseEntity<>(pageData, HttpStatus.OK);
    }

    @Log("根据AgentCode下载日志信息")
    @ApiOperation("根据AgentCode下载日志信息")
    @GetMapping(value = "/downloadAgentLog")
    public ResponseEntity<Object> downloadAgentLog(HttpServletResponse response, HttpServletRequest request, AgentConf agentConf) {
        this.agentConfService.downloadAgentLog(response, request, agentConf);
        return new ResponseEntity<>(null, HttpStatus.OK);
    }

    /**
     * @Method: downloadAgentLog <br>
     * @Param: [response, request, agentConf] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：查看日志目录<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/6/11 10:29 <br>
     * @Version： V1.0<br>
     */
    @Log("查看日志目录")
    @ApiOperation("查看日志目录")
    @PostMapping(value = "/qryCatalogue")
    public ResponseEntity<Object> qryCatalogue(@RequestBody AgentConf agentConf) {
        List<Catalogue> catalogues = this.agentConfService.qryCatalogue(agentConf);
        return new ResponseEntity<>(catalogues, HttpStatus.OK);
    }

    /**
     * @Method: previewLog <br>
     * @Param: [agentConf] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：<br>
     * @Author： 日志在线预览gc.zuo<br>
     * @Date： 2021/6/16 15:33 <br>
     * @Version： V1.0<br>
     */
    @Log("日志在线预览")
    @ApiOperation("日志在线预览")
    @PostMapping(value = "/previewLog")
    public ResponseEntity<Object> previewLog(@RequestBody AgentConf agentConf) {
        String log = this.agentConfService.previewLog(agentConf);
        return new ResponseEntity<>(log, HttpStatus.OK);
    }

    /**
     * @Method: getAgentPath <br>
     * @Param: [] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：查询agent日志路径<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/7/23 10:25 <br>
     * @Version： V1.0<br>
     */
    @Log("查询agent日志路径")
    @ApiOperation("查询agent日志路径")
    @PostMapping(value = "/getAgentPath")
    public ResponseEntity<Object> getAgentPath(@RequestBody AgentConf agentConf) {
        String agentPath = this.agentConfService.getAgentPath(agentConf);
        return new ResponseEntity<>(agentPath, HttpStatus.OK);
    }

    /**
     * @Method: findNodeByCurrentApp
     * @Param: []
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description： 获取当前应用所属节点列表
     * @Author： jd.chen
     * @Date： 2021/7/12 19:08
     * @Version： V1.0
     */
    @Log("获取当前应用所属节点列表")
    @ApiOperation("获取当前应用所属节点列表")
    @GetMapping(value = "/findNodeByCurrentApp")
    public ResponseEntity<Object> findNodeByCurrentApp(String appCode) {
        log.info("进入了【AgentConfController的findNodeByCurrentApp方法】");
        List<AppAgentVo> data = agentConfService.findNodeByCurrentApp(appCode);
        return new ResponseEntity(data, HttpStatus.OK);
    }

    @Log("获取当前用户所属节点列表")
    @ApiOperation("获取当前用户所属节点列表")
    @GetMapping(value = "/findNodeByCurrentUser")
    public ResponseEntity<Object> findNodeByCurrentUser() {
        log.info("进入了【AgentConfController的findNodeByCurrentUser方法】");
        List<AppAgentVo> list = this.agentConfService.findNodeByCurrentUser();

        return new ResponseEntity(list, HttpStatus.OK);
    }

    @Log("查询内存有效节点")
    @ApiOperation("查询内存有效节点")
    @GetMapping(value = "/qryAgentSession")
    public ResponseEntity<Object> qryAgentSession(AgentSessionVO agentSession, Page page) {
        PageData pageData = new PageData(this.agentConfService.qryAgentSession(agentSession, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @Log("查询内存无效节点")
    @ApiOperation("查询内存无效节点")
    @GetMapping(value = "/qryNotValidAgentSession")
    public ResponseEntity<Object> qryNotValidAgentSession(AgentSessionVO agentSession, Page page) {
        PageData pageData = new PageData(this.agentConfService.qryNotValidAgentSession(agentSession, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @Log("查询内存节点map")
    @ApiOperation("查询内存节点map")
    @GetMapping(value = "/qryAgentSessionMap")
    public ResponseEntity<Object> qryAgentSessionMap(AgentSessionVO agentSession, Page page) {
        PageData pageData = new PageData(this.agentConfService.qryAgentSessionMap(agentSession, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @Log("判断调度服务端口是否存在")
    @ApiOperation("判断调度服务端口是否存在")
    @PostMapping(value = "/checkPort")
    public ResponseEntity<Object> checkPort(@RequestBody AgentConf agentConf) {
        log.debug("进入【AgentConfController的checkPort方法】");
        boolean result = this.agentConfService.checkAgentPort(agentConf);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @AnonymousAccess
    @Log("查询内存AGENT所有信息")
    @ApiOperation("查询内存AGENT所有信息")
    @GetMapping(value = "/qryAgentSessionInfo/{agentCode}")
    public ResponseEntity<Object> qryAgentSessionInfo(@PathVariable("agentCode") String agentCode) {
        return new ResponseEntity(agentConfService.qryAgentSessionInfo(agentCode), HttpStatus.OK);
    }
}
